package com.example.project.history;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.project.R;
import com.example.project.db.MyDBHelper;
import com.example.project.dto.ListnoCategoryDto;

public class HistoryDelActivity extends Activity{
	private MyDBHelper myDbManager;
	private ArrayAdapter<ListnoCategoryDto> adapter;
	private ListView list;
	private Button btndel;
	ArrayList<ListnoCategoryDto> arr_list;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historydelactivity);
		ArrayList<Parcelable> par = getIntent().getParcelableArrayListExtra("arr_list");
		arr_list = (ArrayList<ListnoCategoryDto>) par.clone();
		
		adapter = new ArrayAdapter<ListnoCategoryDto>(this, android.R.layout.
			simple_list_item_multiple_choice, arr_list);
		list=(ListView) findViewById(R.id.list);
		list.setAdapter(adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		btndel = (Button) findViewById(R.id.btndel);
		btndel.setVisibility(Button.VISIBLE);
		
		myDbManager = MyDBHelper.getInstance(this);
	}
	
	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btndel:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			if (sb.size() != 0) {
				for (int i = list.getCount() - 1; i >= 0 ; i--) {
					if (sb.get(i)) {
						myDbManager.delete(arr_list.get(i).getList_no());
					}
				}
				list.clearChoices();
			}
			break;
		}
		Intent intent = new Intent(this, History.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
}
